package com.kun.index.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.common.exception.MyException;
import com.kun.index.controller.dto.LoginDto;
import com.kun.index.controller.dto.UserDto;
import com.kun.index.entity.User;
import com.kun.index.mapper.UserMapper;
import com.kun.index.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-02-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultDto login(LoginDto user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        List<User> users = userMapper.selectList(wrapper);
        if (users.size()>1){
            throw new MyException(Constants.CODE_600,"数据库异常,有用户名相同的用户存在");
        }else if(users.size()==1){
            if (users.get(0).getPassword().equals(user.getPassword())){
                UserDto userDto = new UserDto();
                userDto.setUsername(users.get(0).getUsername());
                userDto.setNickname(users.get(0).getNickname());
                userDto.setId(users.get(0).getId());
                userDto.setAddress(users.get(0).getAddress());
                userDto.setEmail(users.get(0).getEmail());
                userDto.setAvatarUrl(users.get(0).getAvatarUrl());
                userDto.setRole(users.get(0).getRole());
//                userDto.setRememberMe(user.getRememberMe());
                userDto.setToken(TokenUtils.generateToken(""+users.get(0).getId(),users.get(0).getPassword()));
                return ResultDto.success(userDto);
            }
        }
        throw new MyException(Constants.CODE_302,"账号密码错误");

    }

    @Override
    public User queryUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public Integer saveUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public boolean updatePassword(Integer id, String oldPassword, String newPassword) {
        User user = new User();
        user.setId(id);
        user.setPassword(newPassword);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("password",oldPassword)
                .eq("id",id);
        int update = userMapper.update(user, wrapper);
        if (update>0){
            return true;
        }
        return false;
    }

    @Override
    public List<User> queryAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public User queryUserByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }
}
